package com.example.arithmeticleetcode.leetcode.august;

import java.util.*;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-08-07 09:23
 **/
public class Demo05 {

    public static void main(String[] args) {
        List<Character> list = new ArrayList<>();
        for (char c : "hello".toCharArray()) {
            list.add(c);
        }
        System.out.println(list);

        System.out.println("---------------------------------");

//        输入：["bella","label","roller"]
//        输出：["e","l","l"]
        String[] A1 = {"bella","label","roller"};
        System.out.println(commonChars(A1));

//        输入：["cool","lock","cook"]
//        输出：["c","o"]
        String[] A2 = {"cool","lock","cook"};
        System.out.println(commonChars(A2));


    }

    public static List<String> commonChars(String[] A) {
        List<String> result = new ArrayList<>();
        if (A.length == 1) {
            for (int i = 0, len = A[0].length(); i < len; i++) {
                result.add(String.valueOf(A[0].charAt(i)));
            }
            return result;
        }


        List<List<Character>> list = new ArrayList<>();
        for (String s : A) {
            List<Character> tempList = new ArrayList<>();
            for (char c : s.toCharArray()) {
                tempList.add(c);
            }
            Collections.sort(tempList);
            list.add(tempList);
        }

        List<Character> firstCharacters = list.get(0);
        for (Character firstCharacter : firstCharacters) {
            boolean flag = true;
            for (int i = 1, len = list.size(); i < len; i++) {
                if (!list.contains(firstCharacter)) {
                    flag = false;
                }
            }
            if (flag) {
                result.add(String.valueOf(firstCharacter));
            }
        }


        return result;
    }
}
